package com.zheng.api.server.service.base.pay;

import com.zheng.common.constant.PayConstant;
import com.zheng.pay.dao.model.PayTransOrder;
import com.zheng.pay.dao.model.PayTransOrderExample;
import com.zheng.pay.rpc.api.PayTransOrderService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

/**
 * @author: lvb
 * @date: 17/10/30
 * @description:
 */
@Service
public class Base4TransOrderService extends BaseCommonService {

    @Autowired
    private PayTransOrderService payTransOrderService;

    public int baseCreateTransOrder(PayTransOrder transOrder) {
        return payTransOrderService.insertSelective(transOrder);
    }

    public PayTransOrder baseSelectTransOrder(String transOrderId) {
        PayTransOrderExample example = new PayTransOrderExample();
        PayTransOrderExample.Criteria criteria = example.createCriteria();
        criteria.andTransorderidEqualTo(transOrderId);
        return payTransOrderService.selectFirstByExample(example);
    }

    public PayTransOrder baseSelectByMchIdAndTransOrderId(String mchId, String transOrderId) {
        PayTransOrderExample example = new PayTransOrderExample();
        PayTransOrderExample.Criteria criteria = example.createCriteria();
        criteria.andMchidEqualTo(mchId);
        criteria.andTransorderidEqualTo(transOrderId);
        List<PayTransOrder> transOrderList = payTransOrderService.selectByExample(example);
        return CollectionUtils.isEmpty(transOrderList) ? null : transOrderList.get(0);
    }

    public PayTransOrder baseSelectByMchIdAndMchTransNo(String mchId, String mchTransNo) {
        PayTransOrderExample example = new PayTransOrderExample();
        PayTransOrderExample.Criteria criteria = example.createCriteria();
        criteria.andMchidEqualTo(mchId);
        criteria.andMchtransnoEqualTo(mchTransNo);
        List<PayTransOrder> transOrderList = payTransOrderService.selectByExample(example);
        return CollectionUtils.isEmpty(transOrderList) ? null : transOrderList.get(0);
    }

    public int baseUpdateStatus4Ing(String transOrderId, String channelOrderNo) {
        PayTransOrder transOrder = new PayTransOrder();
        transOrder.setStatus(PayConstant.TRANS_STATUS_TRANING);
        if(channelOrderNo != null) transOrder.setChannelorderno(channelOrderNo);
        transOrder.setTranssucctime(new Date());
        PayTransOrderExample example = new PayTransOrderExample();
        PayTransOrderExample.Criteria criteria = example.createCriteria();
        criteria.andTransorderidEqualTo(transOrderId);
        List<Byte> list = new LinkedList<>();
        list.add(PayConstant.TRANS_STATUS_INIT);
        list.add(PayConstant.TRANS_STATUS_FAIL);
        criteria.andStatusIn(list);
        return payTransOrderService.updateByExampleSelective(transOrder, example);
    }

    public int baseUpdateStatus4Success(String transOrderId) {
        return baseUpdateStatus4Success(transOrderId, null);
    }

    public int baseUpdateStatus4Success(String transOrderId, String channelOrderNo) {
        PayTransOrder transOrder = new PayTransOrder();
        transOrder.setTransorderid(transOrderId);
        transOrder.setStatus(PayConstant.TRANS_STATUS_SUCCESS);
        transOrder.setResult(PayConstant.TRANS_RESULT_SUCCESS);
        transOrder.setTranssucctime(new Date());
        if(StringUtils.isNotBlank(channelOrderNo)) transOrder.setChannelorderno(channelOrderNo);
        PayTransOrderExample example = new PayTransOrderExample();
        PayTransOrderExample.Criteria criteria = example.createCriteria();
        criteria.andTransorderidEqualTo(transOrderId);
        criteria.andStatusEqualTo(PayConstant.TRANS_STATUS_TRANING);
        return payTransOrderService.updateByExampleSelective(transOrder, example);
    }

    public int baseUpdateStatus4Complete(String transOrderId) {
        PayTransOrder transOrder = new PayTransOrder();
        transOrder.setTransorderid(transOrderId);
        transOrder.setStatus(PayConstant.TRANS_STATUS_COMPLETE);
        PayTransOrderExample example = new PayTransOrderExample();
        PayTransOrderExample.Criteria criteria = example.createCriteria();
        criteria.andTransorderidEqualTo(transOrderId);
        List values = CollectionUtils.arrayToList(new Byte[] {
                PayConstant.TRANS_STATUS_SUCCESS, PayConstant.TRANS_STATUS_FAIL
        });
        criteria.andStatusIn(values);
        return payTransOrderService.updateByExampleSelective(transOrder, example);
    }

    public int baseUpdateStatus4Fail(String transOrderId, String channelErrCode, String channelErrMsg) {
        PayTransOrder transOrder = new PayTransOrder();
        transOrder.setStatus(PayConstant.TRANS_STATUS_FAIL);
        transOrder.setResult(PayConstant.TRANS_RESULT_FAIL);
        if(channelErrCode != null) transOrder.setChannelerrcode(channelErrCode);
        if(channelErrMsg != null) transOrder.setChannelerrmsg(channelErrMsg);
        PayTransOrderExample example = new PayTransOrderExample();
        PayTransOrderExample.Criteria criteria = example.createCriteria();
        criteria.andTransorderidEqualTo(transOrderId);
        criteria.andStatusEqualTo(PayConstant.TRANS_STATUS_TRANING);
        return payTransOrderService.updateByExampleSelective(transOrder, example);
    }

}
